<?php

namespace App\Http\Controllers\WeChat;

use App\Http\Controllers\Controller;

class WeChatMenuController extends Controller
{
   /**
    * 微信菜单
    */

    function api()
    {
        //获取随机字符串
        $echoStr = $_GET["echostr"];
        //微信服务器进行效验
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    function checkSignature()
    {
        $signature = $_GET["signature"]; //微信加密签名
        $timestamp = $_GET["timestamp"]; //时间戳
        $nonce = $_GET["nonce"]; //随机数
        $token = "wechat"; //token 验证
        $tmpArr = array($token, $timestamp, $nonce);
        //进行字典排序 2
        sort($tmpArr, SORT_STRING);
        //将三个字符串拼接一个字符串
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr ); //进行加密
        //开发者获得加密后的字符串可与signature对比
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }

    }
}
